import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import BookDetailView from '../views/BookDetailView.vue'
import PaymentView from '../views/PaymentView.vue'
import TransactionHistory from '../views/TransactionHistory.vue'
import MyBooksView from '../views/MyBooksView.vue'
import LoginView from '../views/LoginView.vue'
import MyReportsView from '../views/MyReportsView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: { requiresStudent: true }
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
    {
      path: '/admin/dashboard',
      name: 'admin-dashboard',
      component: () => import('../views/admin/DashboardView.vue'),
      meta: { requiresAdmin: true }
    },
    {
      path: '/admin/users',
      name: 'admin-users',
      component: () => import('../views/admin/DashboardView.vue'),
      meta: { requiresAdmin: true, showUserManagement: true }
    },
    {
      path: '/admin/categories',
      name: 'AdminCategories',
      component: () => import('../views/admin/DashboardView.vue'),
      meta: { requiresAdmin: true, showCategoryManagement: true }
    },
    {
      path: '/admin/settings',
      name: 'AdminSettings',
      component: () => import('../views/admin/DashboardView.vue'),
      meta: { requiresAdmin: true, showSettings: true }
    },
    {
      path: '/admin/books',
      name: 'AdminBooks',
      component: () => import('../views/admin/DashboardView.vue'),
      meta: { requiresAdmin: true, showBookManagement: true }
    },
    {
      path: '/admin/reports',
      name: 'AdminReports',
      component: () => import('../views/admin/DashboardView.vue'),
      meta: { requiresAdmin: true, showReportManagement: true }
    },
    {
      path: '/book/:id',
      name: 'bookDetails',
      component: BookDetailView,
    },
    {
      path: '/publish',
      name: 'publish-book',
      component: () => import('../views/PublishBookView.vue'),
    },
    {
      path: '/edit-book/:id',
      name: 'edit-book',
      component: () => import('../views/PublishBookView.vue'),
    },
    {
      path: '/search',
      name: 'search-results',
      component: () => import('../views/SearchResultsView.vue'),
    },
    {
      path: '/category',
      name: 'category',
      component: () => import('../views/CategoryView.vue'),
    },
    {
      path: '/my-books',
      name: 'myBooks',
      component: MyBooksView,
    },
    {
      path: '/messages',
      name: 'messages',
      component: () => import('../views/MessagesView.vue'),
    },
    {
      path: '/user',
      name: 'user-profile',
      component: () => import('../views/UserProfileView.vue'),
    },
    {
      path: '/payment/:bookId',
      name: 'payment',
      component: PaymentView,
    },
    {
      path: '/transactions',
      name: 'transactions',
      component: TransactionHistory,
    },
    {
      path: '/my-reports',
      name: 'my-reports',
      component: MyReportsView,
    },
  ],
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  const userRole = localStorage.getItem('userRole')
  const isAdmin = userRole === 'admin' || userRole === 'ADMIN'

  // 检查是否已登录
  if (isLoggedIn) {
    // 已登录用户访问登录页面时重定向
    if (to.path === '/login') {
      if (isAdmin) {
        next('/admin/dashboard')
      } else {
        next('/')
      }
      return
    }

    // 管理员只能访问/admin开头的路由
    if (isAdmin) {
      if (!to.path.startsWith('/admin') && to.meta.requiresStudent) {
        next('/admin/dashboard')
        return
      }
    }
    
    // 学生不能访问管理员页面
    if (!isAdmin && to.path.startsWith('/admin')) {
      next('/')
      return
    }
  }

  // 需要管理员权限的路由
  if (to.meta.requiresAdmin) {
    // 检查是否登录且是管理员
    if (isLoggedIn && isAdmin) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
